package main

import (
	"os"
	"bytes"
	"io/ioutil"
	"go/ast"
	"go/parser"
	"go/token"
	"go/printer"
)

type File struct {
	name string
	ast  *ast.File
	fs   *token.FileSet
}

func parse(fname string) (*File, os.Error) {
	fs := token.NewFileSet()
	file, err := parser.ParseFile(fs, fname, nil, parser.ParseComments)
	if err != nil {
		return nil, err
	}
	f := &File{
		name: fname,
		ast:  file,
		fs:   fs,
	}
	return f, nil
}

func (file *File) write() (string, os.Error) {
	var buf bytes.Buffer
	if err := printer.Fprint(&buf, file.fs, file.ast); err != nil {
		return "", err
	}
	fname := "dbgo_" + file.name
	if err := ioutil.WriteFile(fname, buf.Bytes(), 0666); err != nil {
		return "", err
	}
	return fname, nil
}
